import os.path

import openpyxl
from openpyxl.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet

from common.conf import data_path
from common.doLog import DoLog

doLog=DoLog()
myLog=doLog.getLogger(__name__)
#文件处理
class DoFiles():
    #行为读
    def readTxt(self,fname):
        #读txt文件
        # myFile=rf'D:\pythno work\datas\{fname}'
        myFile=os.path.join(data_path,fname)
        with open(myFile,mode='r',encoding='utf-8') as f:
            readStr=f.read()
            print(readStr)
        return readStr
    #
    # #写文件（可存在，或不存在）
    def writeTxt(self, fname,str):
        # myFile = rf'D:\pythno work\datas\{fname}'
        myFile=os.path.join(data_path,'fname')
        with open(myFile,mode='w',encoding='utf-8') as f:
            writeStr = f.write()
            print(writeStr)
        return writeStr

    #读excel文件
    # def readExcel(self,fname,stname):
    #     # myFile = rf'D:\pythno work\datas\{fname}'
    #     myFile=os.path.join(data_path,fname)
    #     # #获取工作簿对象
    #     wk:Workbook=openpyxl.load_workbook(myFilee,read_only=True)
    #     # #获取sheet页对象
    #     st:Worksheet=wk[stname]
    #     # #读取sheet中的内容
    #     vs=st.values
    #     print(st.values)
    #     vs=[i for i in st.values]
    #     print(vs)
    #     return vs
    def readExcelDiv(self,fname,stname,minR,maxR,minC,maxC):
        try:
            # myFilee = rf'D:\2504\autoTest\datas\{fname}'
            myFilee=os.path.join(data_path,fname)
            #获取工作簿对象
            wk:Workbook=openpyxl.load_workbook(myFilee,read_only=True)
            #获取sheet页对象
            st:Worksheet=wk[stname]
        except Exception as e:
            print(f'读取文件异常，异常问题：{e}')
            # 读取部分
            # vs=st.iter_rows(2,8,2,3)
        else:
            vs=[i for i in st.iter_rows(minR,maxR,minC,maxC,values_only=True)]
            print(vs)
            print('未发生异常')
            return vs

    # def writeExcel(self,fname,stname):
    #     #获取工作簿对象
    #     wk:Workbook=openpyxl.Workbook()
    #
    #     #创建sheet对象
    #     st: Worksheet=wk.create_sheet(stname)
    #
    #     #写内容
    #     st.cell(1,1,'123456')
    #     st.cell(1,2,'gggggg')
    #     st.cell(2,1,'abcdef')
    #     st.cell(2,2,'789456')
    #
        #保存
        # myFile = os.path.join(data_path,fname)
        # wk.save(myFile)

if __name__=='__main__':
    r=DoFiles()
    # r.readTxt('aaa.txt')
    # r.readExcel('asas.xlsx')
    # r.readExcelDiv('asas.xlsx',1,4,2,3)
    # r.writeExcel('gyh.xlsx')

    #读
    #获取文件对象   r对后面的字符串   反转义
    # #读txt文件
    # myFilee=os.path.join(data_path,fname)
    # f=open(myFilee,mode='r',encoding='utf-8')
    # print(f.read())
    # #只读当前行
    # print(f.readline())
    # f.close()

    #写文件（可存在，或者不存在）
    #如果存在:覆盖重新写
    #如果不存在：创建
    myFilee = r'D:\pythno work\datas\bbb.txt'
    f=open(myFilee, mode='w', encoding='utf-8')
    f.write('aaa\nbbb\nccc\nddd\nffff')

    f.close()

    #写文件-追加
    # myFilee = r'D:\pythno work\datas\aaa.txt'
    # f = open(myFilee, mode='a', encoding='utf-8')
    # f.write('aaa1111\nbbb222\nccc333\nddd4444\nffff5555')
    #
    # f.close()
